package com.sctvcloud.zhaohua_sd.ui.dialog;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;

import com.jiongbull.jlog.JLog;
import com.ruihang.generalibrary.utils.DensityUtil;
import com.ruihang.generalibrary.ui.dialog.AlertDialogFragment;
import com.sctvcloud.zhaohua_sd.R;
import com.sctvcloud.zhaohua_sd.ui.utils.update.DownloadManager;

/**
 * Created by Administrator on 2017/5/17.
 */

public class UpdateDialogFragment extends AlertDialogFragment implements AlertDialogFragment.OnViewClick {


    DownloadManager manager = null;
    private boolean isUpdate = false;
    private boolean isForce = false;
    private boolean isOkCalled;

    @Override
    protected void initViews(Dialog dialog, Bundle savedInstanceState) {
        super.initViews(dialog, savedInstanceState);
        setCancelable(false);
        setTitleStr(getString(R.string.soft_update_title));
        setOkStrRes(R.string.soft_update_updatebtn);
        if (isForce) {
            setCancleStrRes(R.string.soft_update_exit);
        } else {
            setCancleStrRes(R.string.soft_update_later);
        }
        msg.setGravity(Gravity.LEFT);
        int p = DensityUtil.dip2px(getContext(), 5);
        msg.setPadding(p, 0, p, 0);
    }

    @Override
    public boolean onViewClick(View view, int viewItem, int mode) {
        if (viewItem == VIEW_ITEM_OK) {
            isOkCalled = true;
            if (manager != null) {
                JLog.e("self view click");
                manager.onUpdateAction(getActivity().getSupportFragmentManager());
            }
        } else if (viewItem == VIEW_ITEM_CANCLE) {
            if (!isForce) {
                isUpdate = false;
            }
            return true;
        }
        return false;
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        if (isOkCalled) {
            isOkCalled = false;
        } else {
            if (isForce) {
                JLog.e("will exit");
//                System.exit(0);
            }
        }
    }

    public UpdateDialogFragment setForce(boolean force) {
        isForce = force;
        return this;
    }

    public boolean isForce() {
        return isForce;
    }
}
